import { initSearchConfig, TableSearchPanel, XxpageContainer, XxRangePicker, XxTable } from "@/ui";
import React, { useEffect, useRef, useState } from "react";
import searchFields from "./config/searchFields";
import getActoinPanel from "./config/action";
import getColumns from "./config/columns";
import { mobxState } from "@/data";
import { fileApi } from "@/api";
import { deleteConfirm, successAlert } from "@/common/methods";


/**@description 素材列表页面 */
const FileManagerList = () => {
    const menuFormRef = useRef(null);
    const [params, setParams] = useState({ ...initSearchConfig(searchFields) })
    const [state, setState] = useState({
        data: [],
        totalCount: 0
    })
    const [loading, setLoading] = useState(false);

    //搜索事件
    const onClickSearch = (value) => {
        setParams({
            ...params,
            ...value,
            current: 1,
        });
    };


    /**@description 查找文件列表 */
    const getList = async () => {
        setLoading(true);
        const { data, code, msg } = await fileApi.getFileList({ ...params });
        setState({
            totalCount: data.totalCount,
            data: [...data.list]
        })
        setLoading(false);

    }

    const onChange = (current, pageSize) => {
        setParams({
            ...params,
            current,
            pageSize
        })
    }
    useEffect(() => {
        getList()
    }, [params])

    /**  @description 打开编辑文件窗口 */
    const open = (type = "add", id, parentId) => {
        mobxState.onChangeTab({
            label: "产品编辑",
            key: "product_type_edit_form",
            path: "/type/product/edit",
            state: {
            }
        })
    }

    /**@description 删除按钮操作 */
    const handleDelete = async (record) => {
        try {
            await deleteConfirm();
            const { code, msg } = await fileApi.deleteProd(record.id)
        } catch {

        } finally {
            getList()
        }
    }


    const [selectedKeys, setSelectedKeys] = useState([])
    const onSelectChange = (newSelectedRowKeys, selectedRows) => {
        setSelectedKeys(newSelectedRowKeys);
    }
    const actionPanel = getActoinPanel({
        editProductForm: open
    })
    const tableProps = {
        tablePanelConfig: actionPanel,
        showActionPanel: true,
        loading,
        columns: getColumns({ open, handleDelete }),
        dataSource: state.data,
        pagination: {
            total: state.totalCount,
            pageSize: params.pageSize,
            onChange: onChange,
            current: params.current

        },
        rowSelection: {
            type: "checkbox",
            selectedRowKeys: selectedKeys,
            onChange: onSelectChange,
        },
        rowKey: 'id',
        selectedRowKeys: selectedKeys,
        onRow: (record, index) => {
            return {
                onDoubleClick: () => {
                    console.log("双击", record);
                }
            }
        },
    }

    return (
        <XxpageContainer>
            <TableSearchPanel
                fields={searchFields}
                onSearch={onClickSearch}

            />
            <XxTable  {...tableProps} />
        </XxpageContainer>
    )
}

export default FileManagerList;